package org.algorithm.熊猫.链表;

import org.algorithm.common.ListNode;
import org.junit.Test;

/**
 * @Date 2022/2/16 17:17
 */
public class 快慢指针寻找中间节点 {

    @Test
    public void test() {
        ListNode node = ListNode.build(1, 2);
        ListNode.print(node);
        System.out.println();

        System.out.println(middleNode(node));
    }


    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode last = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            last = last.next;
        }
        return last;
    }
}
